//
//  Observable+Bookstore.swift
//  SLQuickReader
//
//  Created by ALin on 2017/8/3.
//  Copyright © 2017年 ALin. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa
import SwiftyJSON
import Moya
import Then

let male = "male" // 男频
let female = "female" // 女频
let picture = "picture" // 漫画
let press = "press" // 出版图书
let maleTitle = "丨男生频道丨"
let femaleTitle = "丨女生频道丨"
let pictureTitle = "丨动漫频道丨"
let pressTitle = "丨出版图书丨"


enum RankingGender: String {
    case female = "female"
    case male = "male"
}

extension Observable where Element: Moya.Response{
    // MARK:- Ranking
    func parseRankings(gender: RankingGender) -> Observable<[Rankinger]> {
        return map{ response in
            
            // 判断请求状态
            guard (200...209) ~= response.statusCode else{
                throw ORMError.httperror
            }
            
            UserDefaults.standard.do{
                $0.setValue(response.data, forKey: "rankingerjson")
                $0.synchronize()
            }
            return RankingViewModel.parseRankingData(data: response.data, gender: .male)
            
        }
    }
    
    // MARK:- Recommend
    func parse2Books(args: String = "") -> Observable<[Book]> {
        return map{ response in
            
            // 判断请求状态
            guard (200...209) ~= response.statusCode else{
                throw ORMError.httperror
            }
            
            let jsonData = JSON.init(data: response.data)
            
            var array = [Book]()
            var books = [JSON]();
            if args.characters.count > 0{
                guard let bookArr = jsonData[args]["books"].array else{
                    return array
                }
                books = bookArr
            }else{
                guard let bookArr = jsonData["books"].array else{
                    return array
                }
                books = bookArr
            }
            
            for dict in books{
                array.append(Book(jsonData: dict)!)
            }
            
            return array
            
        }
    }
    
    // MARK:- Statistics
    /// response 转 Statistics分类
    func parseStatistics() -> Observable<[[String: [Statistics]]]> {
        return map{ response in

            // 判断请求状态
            guard (200...209) ~= response.statusCode else{
                throw ORMError.httperror
            }
            // 保存json
            UserDefaults.standard.do{
                $0.setValue(response.data, forKey: "statistics")
                $0.synchronize()
            }
            
            // ③ 将内容转换成json
            return self.data2Array(response.data)
        }
    }
    
}

extension Observable where Element == Data{
    func parseLocalStatistics() -> Observable<[[String: [Statistics]]]> {
        return map{  self.data2Array($0) }
    }
}

extension Observable{
    // MARK:- private method
    fileprivate func data2Array(_ data: Data) -> [[String: [Statistics]]]{
        let jsonData = JSON.init(data)
        
        var results = [[String: [Statistics]]]()
        
        guard let males = jsonData[male].array else{
            return results
        }
        
        results.append(["丨男生频道丨": parseStatisticsModels(males)])
        
        guard let females = jsonData[female].array else{
            return results
        }
        
        results.append(["丨女生频道丨": parseStatisticsModels(females)])
        
        guard let pictures = jsonData[picture].array else{
            return results
        }
        
        results.append(["丨动漫频道丨": parseStatisticsModels(pictures)])
        
        guard let presses = jsonData[press].array else{
            return results
        }
        
        results.append(["丨出版图书丨": parseStatisticsModels(presses)])
        
        return results
    }
    
    fileprivate func parseStatisticsModels(_ json: [JSON])->[Statistics]{
        var staticses = [Statistics]()
        for dict in json{
            staticses.append(Statistics(jsonData: dict)!)
        }
        return staticses
    }

}


